import 'package:shared_preferences/shared_preferences.dart';
import 'directory_access_service.dart';

class SettingsService {
  static const String _defaultDirectoryKey = 'default_directory';
  static SharedPreferences? _prefs;

  static Future<void> init() async {
    _prefs ??= await SharedPreferences.getInstance();
  }

  static Future<String?> getDefaultDirectory() async {
    await init();
    final directory = _prefs?.getString(_defaultDirectoryKey);
    
    // 如果有保存的目录，尝试恢复访问权限
    if (directory != null) {
      await DirectoryAccessService.restoreDirectoryBookmark(directory);
    }
    
    return directory;
  }

  static Future<bool> setDefaultDirectory(String? directory) async {
    await init();
    if (directory == null) {
      return _prefs?.remove(_defaultDirectoryKey) ?? false;
    }
    
    // 保存目录访问权限书签
    await DirectoryAccessService.saveDirectoryBookmark(directory);
    
    return _prefs?.setString(_defaultDirectoryKey, directory) ?? false;
  }
}